package com.xsjl.user.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsjl.user.mapper.AdminMapper;
import com.xsjl.user.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * admin服务层
 * 
 * @author Administrator
 *
 */
@Service
public class AdminService {

	@Autowired
	private AdminMapper adminMapper;

	@Autowired
	private BCryptPasswordEncoder passwordEncoder;

	public Admin adminLogin(String loginname, String password) {
		Admin admin = adminMapper.findByName(loginname);
		if (admin != null && passwordEncoder.matches(password, admin.getPassword())) {
			return admin;
		}
		return null;
	}

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Admin> findAll() {
		return adminMapper.selectList(null);
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Admin> findSearch(Map whereMap, int page, int size) {
		Page<Admin> pages = new Page<>(page, size);
		QueryWrapper<Admin> wrapper = new QueryWrapper<>();
		wrapper.allEq(whereMap);
		return adminMapper.selectPage(pages, wrapper);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Admin> findSearch(Map whereMap) {
		QueryWrapper<Admin> wrapper = new QueryWrapper<>();
		wrapper.allEq(whereMap);
		return adminMapper.selectList(wrapper);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Admin findById(String id) {
		return adminMapper.selectById(id);
	}

	/**
	 * 增加
	 * @param admin
	 */
	public void add(Admin admin) {

		admin.setPassword(passwordEncoder.encode(admin.getPassword()));
		adminMapper.insert(admin);
	}

	/**
	 * 修改
	 * @param admin
	 */
	public void update(Admin admin) {
		adminMapper.updateById(admin);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		adminMapper.deleteById(id);
	}

}
